import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    name:"home",
    redirect:"/login",
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('@/views/login/Login')
  },
  {
    path:'/register',
    name:'register',
    component:()=>import('@/views/login/register')
  },
  {
    path:'/main',
    name:'main',
    component:()=>import('@/views/main/index'),
    meta:{requireAuth:true},
    children:[
      {
        path:'userList',
        name:'userList',
        component:()=>import('@/views/center/userList')
    
      },
      {
        path:':id/editUsers',
        component:()=>import('@/views/center/editUsers')
      },
      {
        path:'roleList',
        name:'roleList',
        component:()=>import('@/views/center/roles/roleList')
      },
      {
        path:'addRoles',
        name:'addRoles',
        component:()=>import('@/views/center/roles/addRoles')
      },
      {
        path:'showRoles',
        name:'showRoles',
        component:()=>import('@/views/center/roles/showRoles')
      },
      {
        path:'echarts',
        name:'echarts',
        component:()=>import('@/views/echarts')
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    return next()
  }
  let tokenStr=window.sessionStorage.getItem('token');
  if(!tokenStr){
    return next('/login')
  }
  next();
})

export default router
